<?php

namespace app\api\controller;

use app\admin\model\Card as CardModel;
use app\common\controller\Api;
use think\Env;

/**
 * 银行卡接口
 */
class Card extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 显示银行卡
     *
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $model = new CardModel();
        $data = $model->where(['status' => Env::get('status.normal'), 'user_id' => $user_id])
            ->field('id,bank_name,card_num')
            ->select();
        foreach ($data as &$v) {
            $v['card_num'] = str_pad(substr($v['card_num'], -4), strlen($v['card_num']), ".... .... ....", STR_PAD_LEFT);
        }

        $this->success('获取成功', $data, 1);

    }

    /**
     * 添加银行卡
     */
    public function add()
    {
        $model = new CardModel();
        $data = input();
        $data['user_id'] = $this->auth->id;
        $detail = $this->validate($data, 'Card.validate');
        if ($detail !== true) $this->error($detail, [], 500);
        if ($model->save($data)) {
            $this->success('添加成功！', [], 1);
        }
        $this->error('添加失败！', [], 0);

    }

    /**
     * 删除银行卡
     */
    public function delete()
    {
        $model = new CardModel();
        $id = input('card_id');
        $user_id = $this->auth->id;
        if (empty($id)) {
            $this->error('请选中要删除的银行卡');
        }
        $data = $model->where(['id' => $id, 'user_id' => $user_id])->update(['status' => -1]);
        if ($data) {
            $this->success('删除成功！', [], 1);
        }
        $this->error('删除失败！', [], 0);
    }


}
